import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AdminPanel extends JPanel implements ActionListener{
	private boolean DEBUG = false;
   private JTextField filterText;
	private JPanel format;
	private JTable table;
	private TableRowSorter<Employees> sorter;
	private JButton addClientButton;
	private JButton deleteClientButton;
	private Employees list;
	private JScrollPane scrollPane;
	 
	public AdminPanel(){
		list = new Employees();          
    	this.setBackground(Color.WHITE);
      sorter = new TableRowSorter<Employees>(list);
      table = new JTable(list);
      table.setRowSorter(sorter);
      table.setPreferredScrollableViewportSize(new Dimension(400, 70));
      table.setFillsViewportHeight(true);
      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      
		//When selection changes, outputs status bar for current table location
      table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                    
						  public void valueChanged(ListSelectionEvent event) {
                    		int viewRow = table.getSelectedRow();
                    	
								if (viewRow < 0) {
                    			//Selection got filtered away.
                    			System.out.println("");
                    		} 
						  		else {
                    			int modelRow = table.convertRowIndexToModel(viewRow);
                     		System.out.println( String.format("Selected Row in view: %d. " +"Selected Row in model: %d.", viewRow, modelRow));
                    		}
                    }
      		}
        );
		  JPanel form = new JPanel(new GridLayout(3,2));
        form.setBackground(Color.WHITE);
        
        // search/filter bar
        JLabel l1 = new JLabel("Search By First Name", SwingConstants.TRAILING);
        form.add(l1);
        filterText = new JTextField();
        filterText.setColumns(10);
            //Whenever filterText changes, invoke newFilter.
                filterText.getDocument().addDocumentListener(
                        new DocumentListener() {
                            public void changedUpdate(DocumentEvent e) {
                                newFilter();
                            }
                            public void insertUpdate(DocumentEvent e) {
                                newFilter();
                            }
                            public void removeUpdate(DocumentEvent e) {
                                newFilter();
                            }
                        });
        
        l1.setLabelFor(filterText);
        form.add(filterText);		
			
        addClientButton = new JButton("Test Add");
		  addClientButton.addActionListener(this);
		  deleteClientButton = new JButton("Test Remove");
		  deleteClientButton.addActionListener(this); 
        form.add(addClientButton);
		  form.add(deleteClientButton);
        
        scrollPane = new JScrollPane(table);
        this.add(form);          
        this.add(scrollPane);                   
		 }

	
	protected static ImageIcon createImageIcon(String path) { //used to create tab image
        java.net.URL imgURL = HomePanel.class.getResource(path);
              if (imgURL != null) {
                   return new ImageIcon(imgURL);
              } else {
                   System.err.println("Couldn't find file: " + path);
                   return null;
              }
   }
	
	 private void newFilter() {
        RowFilter<Employees, Object> rf = null;
        try {
            rf = RowFilter.regexFilter(filterText.getText(), 0);
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
    }

	//Note add button does not work unless Employee is just a regular class and has a constructor
	public void actionPerformed(ActionEvent evt){
		
	   if(evt.getSource() == deleteClientButton){
			list.removeAt(table.getSelectedRow());
		}
		/*else if(evt.getSource() == addClientButton){
			Employee e = new Employee("First","Last","ID#","PW:000","JOB");
			list.addEmployee(e);
		}*/
	}

	
}